package com.cheng.controller;

import com.cheng.common.exception.CommonException;
import com.cheng.common.results.DataRslt;
import com.cheng.data.entity.DepartmentEntity;
import com.cheng.data.entity.GvmtTypeInfEntity;
import com.cheng.data.entity.RoleInfoEntity;
import com.cheng.data.vo.ReqPageInfoVO;
import com.cheng.data.vo.RespPageInfoVO;
import com.cheng.data.vo.TreeVO;
import com.cheng.data.vo.UserInfoVO;
import com.cheng.operation.PermInfoOperation;
import com.cheng.operation.UserOperation;
import com.cheng.service.DepartmentService;
import com.cheng.service.GvmtTypeInfService;
import com.cheng.service.RoleInfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @author: ChengJW
 * @date: 2021/11/14 9:45
 * @version: 1.0
 * @description: 用户管理信息处理类
 */
@Controller
public class UserAdminController extends BaseController{

    private final static Logger log = LoggerFactory.getLogger(UserAdminController.class);

    @Autowired
    private UserOperation userOperation;

    @Autowired
    private RoleInfoService roleInfoService;

    @Autowired
    private DepartmentService departmentService;

    @Autowired
    private GvmtTypeInfService gvmtTypeInfService;

    @Autowired
    private PermInfoOperation permInfoOperation;

    @RequestMapping("/systemDefend/userAdmin")
    public String userAdmin(){
        return "userAdmin";
    };

    @RequestMapping("/systemDefend/userAdmin/addUserInfo")
    public String addUserInfo(){
        return "addUserInfo";
    }

    @RequestMapping("/systemDefend/userAdmin/updateUserInfo")
    public String updateUserInfo(){
        return "updateUserInfo";
    }

    @RequestMapping("/systemDefend/userAdmin/showUserPermInfo")
    public String showUserPermInfo(){
        return "showUserPermInfo";
    }

    /**
    * @author: ChengJW
    * @params: [pageInfoVO]
    * @return: com.cheng.common.results.DataRslt
    * @date: 2021/11/14
    * @description: 查询用户信息列表
    */
    @RequestMapping("/systemDefend/userAdmin/queryUserList")
    @ResponseBody
    public DataRslt queryUserList(ReqPageInfoVO pageInfoVO) throws CommonException{

        pageInfoVO.setUserCode(getUrCode());
        RespPageInfoVO respPageInfoVO = userOperation.queryUserList(pageInfoVO);
        return tabSuccess(respPageInfoVO);
    }

    /**
    * @author: ChengJW
    * @params: []
    * @return: com.cheng.common.results.DataRslt
    * @date: 2021/11/14
    * @description: 新增用户信息时，初始化部门信息
    */
    @RequestMapping("/systemDefend/userAdmin/initUserInfoDep")
    @ResponseBody
    public DataRslt initUserInfoDep() throws CommonException{

        DepartmentEntity entity = new DepartmentEntity();
        entity.setDtUpsTypeCode(getUrCode());
        return success(departmentService.selectDepartmentInfo(entity));
    }

    /**
     * @author: ChengJW
     * @params: []
     * @return: com.cheng.common.results.DataRslt
     * @date: 2021/11/14
     * @description: 新增用户信息时，初始化角色信息
     */
    @RequestMapping("/systemDefend/userAdmin/initUserInfoRole")
    @ResponseBody
    public DataRslt initUserInfoRole() throws CommonException{

        RoleInfoEntity entity = new RoleInfoEntity();
        entity.setRoCode(getUrCode());
        return success(roleInfoService.queryRoleInfoList(entity));
    }

    /**
     * @author: ChengJW
     * @params: []
     * @return: com.cheng.common.results.DataRslt
     * @date: 2021/11/14
     * @description: 新增用户信息时，初始化单位信息
     */
    @RequestMapping("/systemDefend/userAdmin/initUserInfoDwCode")
    @ResponseBody
    public DataRslt initUserInfoDwCode() throws CommonException{

        GvmtTypeInfEntity entity = new GvmtTypeInfEntity();
        entity.setGtTypeCode(getUrCode());
        List<GvmtTypeInfEntity> list = gvmtTypeInfService.queryGvmtTypeInf(entity);
        if (null != list && list.size() > 0){
            return success(list.get(0));
        }
        return null;
    }

    /**
    * @author: ChengJW
    * @params:
    * @return:
    * @date: 2021/11/14
    * @description: 新增用户信息
    */
    @RequestMapping("/systemDefend/userAdmin/insertUserInfo")
    @ResponseBody
    public DataRslt insertUserInfo(UserInfoVO userInfoVO) throws CommonException{

        userOperation.insertUserInfo(userInfoVO);
        return success();
    }

    /**
    * @author: ChengJW
    * @params: [userInfoVO]
    * @return: com.cheng.common.results.DataRslt
    * @date: 2021/11/14
    * @description: 修改用户信息
    */
    @RequestMapping("/systemDefend/userAdmin/updateUserInf")
    @ResponseBody
    public DataRslt updateUserInf(UserInfoVO userInfoVO) throws CommonException{

        userOperation.updateUserInfo(userInfoVO);
        return success();
    }

    /**
    * @author: ChengJW
    * @params: [userInfoVO]
    * @return: com.cheng.common.results.DataRslt
    * @date: 2021/11/15
    * @description: 删除用户信息
    */
    @RequestMapping("/systemDefend/userAdmin/deleteUserInfo")
    @ResponseBody
    public DataRslt deleteUserInf(UserInfoVO userInfoVO) throws CommonException{

        userOperation.deleteUserInfo(userInfoVO);
        return success();
    }

    /**
    * @author: ChengJW
    * @params: []
    * @return: com.cheng.common.results.DataRslt
    * @date: 2021/11/15
    * @description: 查询所有的权限信息
    */
    @RequestMapping("/systemDefend/userAdmin/showAllUserPermInfo")
    @ResponseBody
    public DataRslt showAllUserPermInfo(String urNm) throws CommonException{

        List<TreeVO> treeVOList = permInfoOperation.queryAllPermInfoList(urNm,"01");
        return success(treeVOList);
    }
}
